package com.itggm.controller;


import com.itggm.pojo.Slide;
import com.itggm.response.ResponseResult;
import com.itggm.service.SlideService;
import com.itggm.utils.AliOSSUtil;
import com.itggm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: ggm
 * @data 2024年11月20日 11:36
 */
@RestController
@RequestMapping("slide")
public class SlideController {
    @Autowired
    private SlideService slideService;
    @Autowired
    private AliOSSUtil aliOSSUtils;

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 分页查询
     */
    @GetMapping("/page")
    public ResponseResult page(Integer page, Integer limit) {
        return slideService.page(page, limit);
    }


    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 上传图片
     */
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file) throws IOException {
        //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        String url = aliOSSUtils.upload(file);
        //将图片上传完成后的url返回，用于浏览器回显展示
        return ResponseResult.success().data(url);
    }

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 添加图片
     */
    @PostMapping("/uploadInfo")
    public ResponseResult uploadInfo(Slide slide) {
        return slideService.uploadInfo(slide);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 删除轮播图
     */
    @PostMapping("/delete")
    public ResponseResult delete(String uuid) {
        String fileName = StringUtil.extractFileNameFromUrl(uuid);
        try {
            //从阿里云删除文件
            aliOSSUtils.delete(fileName);
            //从数据库删除文件
            slideService.delete(uuid);
            return ResponseResult.success().message("文件删除成功");
        } catch (Exception e) {
            return ResponseResult.fail().message("文件删除失败");
        }
    }

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 置顶操作
     */
    @PostMapping("update")
    public ResponseResult top(Integer carouseId) {
        return slideService.update(carouseId);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 前端页面轮播图展示
     */
    @GetMapping("/font/list")
    public ResponseResult show(Integer limit) {
        return slideService.show(limit);
    }
     /**
      * @Author: ggm
      * @date:  2024/11/26
      * 修改轮播图片
      */
     @PostMapping("/uploadImg")
     public ResponseResult update(Integer carouseId,String fileName,String uuid) {
         return slideService.uploadImg(carouseId,fileName,uuid);

     }



}
